<?php

namespace App\Http\Controllers;

use App\Student;
use http\Env\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class StudentController extends Controller
{
    protected $redirectTo = '/student/';
    
    protected $guard = 'student_admin';

    public function __construct()
    {
        $this->middleware('guest')->except('/student/logout');
    }

    public function login(Request $request)
    {
        $data = $this->validate($request,
            [
                'name' => 'required|string',
                'password' => 'required|string'
            ]);
        if (Auth::guard('student_admin')->attempt($data)){
            return redirect()->route('student_home');
        }
        dd(auth()->user(), 0);
    }

    public function guard()
    {
        return Auth::guard('student_admin');
    }

    public function loginForm()
    {
        return view('student.login');
    }

    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->invalidate();
        return redirect('/student/login');
    }
    
    /**
     * visible after login
     *
     * @return int
     */
    public function index()
    {
        if (auth()->guard($this->guard)->check()) {
            dd($this->guard, auth()->guard($this->guard)->user());
        }
        dd(auth()->guard('student_admin')->user());
        return 1234456;
    }
    public function test()
    {
        dd(app());
    }
    public function loadRegister()
    {
        return view('student.register');
    
    }
    public function register(Request $request)
    {
        $data = $this->validate(
            $request,
            [
                'name' => 'String|required',
                'password' => 'String|required',
            ]
        );
        
        $student = new Student();
        if ($student) {
            return \response('success');
        }
        return \response('failed');
    }

}
